package com.teng.mylibrary.widget

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.teng.mylibrary.ui.adapter.MultipleChooseAdapter
import com.teng.mylibrary.base.BaseDialog
import com.teng.mylibrary.databinding.DialogChooseMultipleBinding
import com.teng.mylibrary.util.ToastHelper

/**
 * Description:  多选选择框
 * Author:       liuteng
 * CreateDate:   2021/9/15 15:07
 * UpdateRemark: 更新说明
 */
class MultipleChooseDialog(val title: String, val list: ArrayList<String>) :
    BaseDialog<DialogChooseMultipleBinding>(DialogChooseMultipleBinding::inflate) {

    var mOnItemOkListener: ((positonStr: String) -> Any?)? = null

    val adapter by lazy {
        MultipleChooseAdapter(list)
    }

    override fun initView(view: View?) {
        binding.tvTitle.text = title
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.recyclerView.adapter = adapter
        adapter.refreshData(list)

        binding.btnOk.setOnClickListener {
            val map = adapter.getSelectedMap()
            if (map.isEmpty()) {
                ToastHelper.showOther(context,"请至少选择一项")
                return@setOnClickListener
            }
            var resultStr = ""
            for ((key, value) in map) {
                resultStr += "${key},"
            }
            mOnItemOkListener?.invoke(resultStr)
            dismiss()
        }
        binding.btnCancel.setOnClickListener {
            dismiss()
        }
    }

    override fun initData() {
    }

    /**
     * 确认
     */
    fun setOnItemOkListener(onItemOkListener: ((positonStr: String) -> Any?)) {
        mOnItemOkListener = onItemOkListener
    }
}